﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using BCCommon.Extensions;
using BCCommon.FreightEnums;
using BCEntity.Common.Area;
using BCCommon;
namespace BCData.Common.Area
{
    public class CityData : ICityData
    {
        private static DateTime? LastLoadOrCheckTime = null;

        private static IEnumerable<CityEntity> CachedRawData;
        private static IDictionary<string, CityEntity> CachedByCityId;

        protected readonly IDatabaseContext databaseContext;

        private const string QUERY = @"SELECT CityId,OrderIndex,CityInitials,CityEasyBackGoods,CreateTime,UpdateTime,Editer,CityNegotiatedPrice,CityNegotiatedPricePartLoad,
                                            Longitude,ProvinceId,ProvinceName,Latitude,IsEnabled,ProvinceCode,CityCode,CityName,CityShortName,CapitalCity,HeatLevel,Municipality 
                                        FROM city";

        public CityData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        private void EnsureCache()
        {
            bool reload = false;

            if (LastLoadOrCheckTime == null)
            {
                reload = true;
            }

            if (LastLoadOrCheckTime < DateTime.Now.AddSeconds(-293))
            {
                // 可能也要考虑 Province.UpdateTime 的。
                var lastUpdateTime = this.databaseContext.Slave.ExecuteScalar<DateTime>("SELECT UpdateTime FROM City ORDER BY UpdateTime DESC LIMIT 1");

                if (lastUpdateTime > LastLoadOrCheckTime)
                {
                    reload = true;
                }
                else
                {
                    LastLoadOrCheckTime = DateTime.Now;
                }
            }

            if (reload)
            {
                LastLoadOrCheckTime = DateTime.Now;

                CachedRawData = this.databaseContext.Slave.Query<CityEntity>("SELECT A.*, B.ProvinceName FROM City A INNER JOIN Province B ON A.ProvinceCode = B.ProvinceCode");

                CachedByCityId = new Dictionary<string, CityEntity>();

                foreach (var item in CachedRawData)
                {
                    CachedByCityId.TryAdd(item.CityCode, item);
                }
            }
        }

        public bool Add(CityEntity entity)
        {
            LastLoadOrCheckTime = null;

            string sql = @"INSERT INTO City(CityCode,CityName,ProvinceId,ProvinceCode,ProvinceName,CapitalCity,HeatLevel,CityNegotiatedPrice,CityNegotiatedPricePartLoad,
                                Municipality,OrderIndex,CityInitials,CityEasyBackGoods,HotCity,Editer,IsEnabled)
                           VALUES(?CityCode,?CityName,?ProvinceId,?ProvinceCode,?ProvinceName,?CapitalCity,?HeatLevel,?CityNegotiatedPrice,?CityNegotiatedPricePartLoad,
                                ?Municipality,?OrderIndex,?CityInitials,?CityEasyBackGoods,?HotCity,?Editer,?IsEnabled);";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CityCode", entity.CityCode, DbType.AnsiString, size: 20);
            parameter.Add("?CityName", entity.CityName, DbType.AnsiString, size: 30);
            parameter.Add("?ProvinceId", entity.ProvinceId, DbType.Int64, size: 20);
            parameter.Add("?ProvinceCode", entity.ProvinceCode, DbType.AnsiString, size: 20);
            parameter.Add("?ProvinceName", entity.ProvinceName, DbType.AnsiString, size: 50);
            parameter.Add("?CapitalCity", entity.CapitalCity, DbType.Boolean);
            parameter.Add("?HeatLevel", entity.HeatLevel, DbType.Int32, size: 11);
            parameter.Add("?Municipality", entity.Municipality, DbType.Boolean);
            parameter.Add("?OrderIndex", entity.OrderIndex, DbType.Int32, size: 11);
            parameter.Add("?CityInitials", entity.CityInitials, DbType.AnsiString, size: 10);
            parameter.Add("?CityEasyBackGoods", entity.CityEasyBackGoods.ToString(), DbType.AnsiString);
            parameter.Add("?HotCity", entity.HotCity, DbType.Boolean);
            parameter.Add("?CityNegotiatedPrice", entity.CityNegotiatedPrice, DbType.Boolean);
            parameter.Add("?CityNegotiatedPricePartLoad", entity.CityNegotiatedPricePartLoad, DbType.Boolean);
            parameter.Add("?IsEnabled", entity.IsEnabled, DbType.Boolean);
            parameter.Add("?Editer", entity.Editer, DbType.AnsiString, size: 20);
            var result = this.databaseContext.Master.Execute(sql, parameter, databaseContext.Transaction);
            return result > 0;
        }

        public bool Update(CityEntity entity)
        {
            LastLoadOrCheckTime = null;

            string sql = @"UPDATE City SET 
                                CityCode=?CityCode,
                                CityName=?CityName,
                                ProvinceId=?ProvinceId,
                                ProvinceCode=?ProvinceCode,
                                ProvinceName=?ProvinceName,
                                CapitalCity=?CapitalCity,
                                HeatLevel=?HeatLevel,
                                Municipality=?Municipality,
                                OrderIndex=?OrderIndex,
                                CityInitials=?CityInitials,
                                CityEasyBackGoods=?CityEasyBackGoods,
                                HotCity=?HotCity,
                                CityNegotiatedPrice=?CityNegotiatedPrice,
                                CityNegotiatedPricePartLoad=?CityNegotiatedPricePartLoad,
                                Editer=?Editer
                            WHERE CityId=?CityId";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CityId", entity.CityId, DbType.Int64, size: 20);
            parameter.Add("?CityCode", entity.CityCode, DbType.AnsiString, size: 20);
            parameter.Add("?CityName", entity.CityName, DbType.AnsiString, size: 30);
            parameter.Add("?ProvinceId", entity.ProvinceId, DbType.Int64, size: 20);
            parameter.Add("?ProvinceCode", entity.ProvinceCode, DbType.AnsiString, size: 20);
            parameter.Add("?ProvinceName", entity.ProvinceName, DbType.AnsiString, size: 50);
            parameter.Add("?CapitalCity", entity.CapitalCity, DbType.Boolean);
            parameter.Add("?HeatLevel", entity.HeatLevel, DbType.Int32, size: 11);
            parameter.Add("?Municipality", entity.Municipality, DbType.Boolean);
            parameter.Add("?OrderIndex", entity.OrderIndex, DbType.Int32, size: 11);
            parameter.Add("?CityInitials", entity.CityInitials, DbType.AnsiString, size: 10);
            parameter.Add("?CityEasyBackGoods", entity.CityEasyBackGoods.ToString(), DbType.AnsiString);
            parameter.Add("?CityNegotiatedPrice", entity.CityNegotiatedPrice, DbType.Boolean);
            parameter.Add("?CityNegotiatedPricePartLoad", entity.CityNegotiatedPricePartLoad, DbType.Boolean);
            parameter.Add("?HotCity", entity.HotCity, DbType.Boolean);
            parameter.Add("?Editer", entity.Editer, DbType.AnsiString, size: 20);
            var result = this.databaseContext.Master.Execute(sql, parameter, databaseContext.Transaction);
            return result != 0;
        }

        public bool Delete(string cityCode)
        {
            LastLoadOrCheckTime = null;

            string sql = @"DELETE FROM City WHERE CityCode=?CityCode";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);
            var i = this.databaseContext.Slave.Execute(sql, parameter);
            return i > 0;
        }
        public CityEntity GetCity(long cityId)
        {
            string sql = string.Format("{0} WHERE CityId=?CityId", QUERY);
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CityId", cityId, DbType.Int64, size: 20);
            return this.databaseContext.Slave.QueryEntity<CityEntity>(sql, parameter);

        }
        public CityEntity GetCity(string cityCode)
        {
            this.EnsureCache();

            if (CachedByCityId.TryGetValue(cityCode, out var value))
            {
                return value;
            }
            else
            {
                string sql = @"SELECT A.CityCode,A.ProvinceCode,B.ProvinceName,A.CityName,A.CapitalCity,A.HeatLevel,
                                A.Municipality,A.OrderIndex,A.CityInitials,A.CityEasyBackGoods,A.HotCity,
                                A.CreateTime,A.UpdateTime,A.Editer,A.CityNegotiatedPrice,A.CityNegotiatedPricePartLoad
                            FROM City A 
                                INNER JOIN Province B ON A.ProvinceCode=B.ProvinceCode 
                            WHERE CityCode=?CityCode";
                DynamicParameters parameter = new DynamicParameters();
                parameter.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);
                return this.databaseContext.Slave.QueryEntity<CityEntity>(sql, parameter);
            }
        }

        public CityEntity GetCapitalCity(string provinceCode)
        {
            this.EnsureCache();

            return CachedRawData.FirstOrDefault(p => p.ProvinceCode == provinceCode && p.CapitalCity == true);
        }

        public IEnumerable<CityEntity> GetCommunities(string provinceCode)
        {
            this.EnsureCache();

            if (string.IsNullOrWhiteSpace(provinceCode))
            {
                return CachedRawData.Where(p => p.ProvinceCode == provinceCode);
            }

            return CachedRawData;
        }
        public IEnumerable<CityEntity> GetCommunities(long provinceId)
        {
            this.EnsureCache();

            return CachedRawData.Where(p => p.ProvinceId == provinceId);


        }
        public bool CheckExistsById(long cityId)
        {
            string sql = @"SELECT 1 FROM City WHERE CityId=?CityId LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CityId", cityId, DbType.Int64);
            var result = this.databaseContext.Slave.ExecuteScalar(sql, parameter);
            return result != null;
        }
        public bool CheckExistsByCode(string cityCode)
        {
            string sql = @"SELECT 1 FROM City WHERE CityCode=?CityCode LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);
            var result = this.databaseContext.Slave.ExecuteScalar(sql, parameter);
            return result != null;
        }

        public bool CheckExistsByName(string provinceCode, string cityCode)
        {
            string sql = @"SELECT 1 FROM City WHERE ProvinceCode=?ProvinceCode AND CityCode=?CityCode LIMIT 1";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);
            parameter.Add("?ProvinceCode", provinceCode, DbType.AnsiString, size: 20);
            var result = this.databaseContext.Slave.ExecuteScalar(sql, parameter);
            return result != null;
        }

        public IEnumerable<CityEntity> GetCommunities(string provinceCode, string cityCode, int index, int size, out int dataCount)
        {
            DynamicParameters parameter = new DynamicParameters();
            string sqlstr = @"SELECT A.CityId,A.CityCode,A.ProvinceCode,B.ProvinceName,A.CityName,A.CapitalCity,A.HeatLevel,
                                    A.Municipality,A.OrderIndex,A.CityInitials,A.CityEasyBackGoods,A.HotCity,
                                    A.CreateTime,A.UpdateTime,A.Editer,A.CityNegotiatedPrice,A.CityNegotiatedPricePartLoad
                                FROM City A 
                                    LEFT JOIN Province B ON A.ProvinceCode=B.ProvinceCode ";
            List<string> wheres = new List<string>();
            if (!string.IsNullOrWhiteSpace(provinceCode))
            {
                wheres.Add(@"A.ProvinceCode=?ProvinceCode");
                parameter.Add("?ProvinceCode", provinceCode, DbType.AnsiString, size: 20);
            }

            if (!string.IsNullOrWhiteSpace(cityCode))
            {
                wheres.Add(@"A.CityCode=?CityCode");
                parameter.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);
            }

            string sql = string.Format(@"{0} {1} LIMIT {2} , {3};SELECT COUNT(1) FROM City A LEFT JOIN Province B ON A.ProvinceCode=B.ProvinceCode {1}",
                  sqlstr,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (index == 0 ? 0 : index - 1) * size,
                  size);


            var result = this.databaseContext.Slave.QueryMultiple<CityEntity, int>(sql.ToString(), parameter);

            dataCount = result.Item2;

            return result.Item1;
        }

        public IEnumerable<CityEntity> GetCommunitiesInitialsGroup()
        {
            string sql = @"SELECT A.CityId,A.CityCode,A.ProvinceCode,B.ProvinceName,A.CityName,A.CapitalCity,A.HeatLevel,
                                A.Municipality,A.OrderIndex,A.CityInitials,A.CityEasyBackGoods,A.HotCity,
                                A.CreateTime,A.UpdateTime,A.Editer,A.CityNegotiatedPrice,A.CityNegotiatedPricePartLoad
                            FROM City A 
                                LEFT JOIN Province B ON A.ProvinceCode=B.ProvinceCode;";
            return this.databaseContext.Slave.Query<CityEntity>(sql);
        }

        public IEnumerable<CityEntity> GetHotCommunities()
        {
            this.EnsureCache();

            return CachedRawData.Where(p => p.HotCity == true);
        }

        public async Task<int> UpdateCityNegotiatedPrice(string cityCode, FreightBusinessType businessType, bool isNegotiatedPrice)
        {
            LastLoadOrCheckTime = null;

            string sql = string.Empty;
            DynamicParameters parameter = new DynamicParameters();
            if (businessType == FreightBusinessType.PartLoad)
            {
                sql = @"UPDATE City SET 
                                    CityNegotiatedPricePartLoad=?CityNegotiatedPricePartLoad
                                WHERE CityCode=?CityCode;";
                parameter.Add("?CityNegotiatedPricePartLoad", isNegotiatedPrice, DbType.Boolean);
            }
            else
            {
                sql = @"UPDATE City SET 
                                    CityNegotiatedPrice=?CityNegotiatedPrice
                                WHERE CityCode=?CityCode;";
                parameter.Add("?CityNegotiatedPrice", isNegotiatedPrice, DbType.Boolean);
            }

            parameter.Add("?CityCode", cityCode, DbType.AnsiString, size: 20);

            return await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction);

        }

        public async Task<int> UpdateAllCityNegotiatedPrice(string provinceCode, FreightBusinessType businessType, bool isNegotiatedPrice)
        {
            LastLoadOrCheckTime = null;

            string sql = string.Empty;
            DynamicParameters parameter = new DynamicParameters();
            if (businessType == FreightBusinessType.PartLoad)
            {
                sql = @"UPDATE City SET 
                                    CityNegotiatedPricePartLoad=?CityNegotiatedPricePartLoad
                                WHERE ProvinceCode=?ProvinceCode;";
                parameter.Add("?CityNegotiatedPricePartLoad", isNegotiatedPrice, DbType.Boolean);
            }
            else
            {
                sql = @"UPDATE City SET 
                                    CityNegotiatedPrice=?CityNegotiatedPrice
                                WHERE ProvinceCode=?ProvinceCode;";
                parameter.Add("?CityNegotiatedPrice", isNegotiatedPrice, DbType.Boolean);
            }

            parameter.Add("?ProvinceCode", provinceCode, DbType.AnsiString, size: 20);

            return await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction);

        }

        public IEnumerable<CityEntity> GetAllCity()
        {
            this.EnsureCache();

            return CachedRawData;
        }

        public async Task<CityEntity> GetCityAsync(string cityUuid, string cityName, string cityCode, string provinceCode)
        {
            List<string> wheres = new List<string>();
            string sql = @"SELECT A.CityId,A.CityCode,A.ProvinceCode,A.CityName,A.CapitalCity,A.HeatLevel,
                        A.Municipality,A.OrderIndex,A.CityInitials,A.CityEasyBackGoods,A.HotCity,
                        A.CreateTime,A.UpdateTime,A.Editer,A.CityNegotiatedPrice,A.CityNegotiatedPricePartLoad,
                        A.Longitude,A.Latitude,A.CityUuid,A.IsEnabled,B.ProvinceName 
                        FROM City A LEFT JOIN Province B ON A.ProvinceCode = B.ProvinceCode ";
            DynamicParameters parameter = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(cityCode))
            {
                if (cityCode.Length == 2)
                {
                    wheres.Add("SUBSTR(A.CityCode,3,2)=?CityCode");
                }
                else
                {
                    wheres.Add("A.CityCode=?CityCode");
                }
                parameter.Add("?CityCode", cityCode, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(cityName))
            {
                wheres.Add("A.CityName LIKE ?CityName");
                parameter.Add("?CityName", cityName + "%", DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(cityUuid))
            {
                wheres.Add("A.CityUuid=?CityUuid");
                parameter.Add("?CityUuid", cityUuid, DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(provinceCode))
            {
                wheres.Add("A.ProvinceCode=?ProvinceCode");
                parameter.Add("?ProvinceCode", provinceCode, DbType.AnsiString);
            }
            sql = string.Format(@"{0} {1} LIMIT 1", sql, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : string.Empty);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<CityEntity>(sql, parameter);
        }

        public async Task<int> AddCityAsync(CityEntity entity, string batchNo)
        {
            LastLoadOrCheckTime = null;

            string sql = @"INSERT INTO City(CityCode,ProvinceCode,CityName,CityShortName,CapitalCity,HeatLevel,CityNegotiatedPrice,CityNegotiatedPricePartLoad,
                                Municipality,OrderIndex,CityInitials,CityEasyBackGoods,HotCity,Editer,Longitude,Latitude,CityUuid,IsEnabled)
                           VALUES(?CityCode,?ProvinceCode,?CityName,?CityShortName,?CapitalCity,?HeatLevel,?CityNegotiatedPrice,?CityNegotiatedPricePartLoad,
                                ?Municipality,?OrderIndex,?CityInitials,?CityEasyBackGoods,?HotCity,?Editer,?Longitude,?Latitude,UUID(),?IsEnabled);";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?ProvinceCode", entity.ProvinceCode, DbType.AnsiString, size: 20);
            parameter.Add("?CityName", entity.CityName, DbType.AnsiString, size: 30);
            parameter.Add("?CityShortName", "", DbType.AnsiString);
            parameter.Add("?CapitalCity", entity.CapitalCity, DbType.Boolean);
            parameter.Add("?HeatLevel", entity.HeatLevel, DbType.Int32, size: 11);
            parameter.Add("?Municipality", entity.Municipality, DbType.Boolean);
            parameter.Add("?OrderIndex", entity.OrderIndex, DbType.Int32, size: 11);
            parameter.Add("?CityInitials", entity.CityInitials, DbType.AnsiString, size: 10);
            parameter.Add("?CityEasyBackGoods", entity.CityEasyBackGoods.ToString(), DbType.AnsiString);
            parameter.Add("?HotCity", entity.HotCity, DbType.Boolean);
            parameter.Add("?CityNegotiatedPrice", entity.CityNegotiatedPrice, DbType.Boolean);
            parameter.Add("?CityNegotiatedPricePartLoad", entity.CityNegotiatedPricePartLoad, DbType.Boolean);
            parameter.Add("?Editer", entity.Editer, DbType.AnsiString, size: 20);
            parameter.Add("?Longitude", entity.Longitude, DbType.Double);
            parameter.Add("?Latitude", entity.Latitude, DbType.Double);
            parameter.Add("?CityCode", entity.CityCode, DbType.AnsiString);
            parameter.Add("?IsEnabled", entity.IsEnabled, DbType.Boolean);
            var result = await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction) > 0;
            if (!result)
            {
                throw new ArgumentException("添加城市信息失败!");
            }
            sql = this.GetUpdateCityChange(entity, batchNo, out DynamicParameters updateParameter);
            return await this.databaseContext.Master.ExecuteAsync(sql, updateParameter);
        }

        private string GetUpdateCityChange(CityEntity entity, string batchNo, out DynamicParameters updateParameter)
        {
            string sql_cityChange = string.Empty;
            updateParameter = new DynamicParameters();
            sql_cityChange = SqlCommands.GetInsertSql("CityChange",
                new string[] { "BatchNo", "NewCityCode", "NewCityName", "NewCityLongitude", "NewCityLatitude", "NewProvinceCode", "NewProvinceName", "IsNew", "Type", "Content" });
            updateParameter.Add("IsNew", true, DbType.Boolean);
            updateParameter.Add("Content", "写入新市信息 " + entity.CityCode + ":" + entity.CityName, DbType.AnsiString);
            updateParameter.Add("?BatchNo", batchNo, DbType.AnsiString);
            updateParameter.Add("?NewCityName", entity.CityName, DbType.AnsiString);
            updateParameter.Add("?NewCityCode", entity.CityCode, DbType.AnsiString);
            updateParameter.Add("?NewCityLongitude", entity.Longitude, DbType.Double);
            updateParameter.Add("?NewCityLatitude", entity.Latitude, DbType.Double);
            updateParameter.Add("?NewProvinceCode", entity.ProvinceCode, DbType.AnsiString);
            updateParameter.Add("?NewProvinceName", entity.ProvinceName, DbType.AnsiString);
            updateParameter.Add("?Type", "City", DbType.AnsiString);
            return sql_cityChange;
        }

        public async Task<int> UpdateCityAsync(CityEntity entity, string batchNo, CityEntity cityResult, bool? isMatch)
        {
            LastLoadOrCheckTime = null;

            string sql = @"UPDATE City SET 
                                UpdateTime=NOW(),
                                Editer=?Editer,
                                Longitude=?Longitude,
                                Latitude=?Latitude,
                                IsEnabled=?IsEnabled
                            WHERE CityUuid=?CityUuid";
            DynamicParameters parameter = new DynamicParameters();
            parameter.Add("?Editer", entity.Editer, DbType.AnsiString, size: 20);
            parameter.Add("?Longitude", entity.Longitude.Value, DbType.Double);
            parameter.Add("?Latitude", entity.Latitude.Value, DbType.Double);
            parameter.Add("?IsEnabled", entity.IsEnabled.Value, DbType.Boolean);
            parameter.Add("?CityUuid", entity.CityUuid, DbType.AnsiString);
            var result = await this.databaseContext.Master.ExecuteAsync(sql, parameter, databaseContext.Transaction);
            if (result>0&&isMatch.HasValue && !isMatch.Value)
            {
                sql = this.GetUpdateCityChange(entity, batchNo, cityResult, out DynamicParameters updateParameter);
                return await this.databaseContext.Master.ExecuteAsync(sql, updateParameter, databaseContext.Transaction);
            }
            return result;
        }

        private string GetUpdateCityChange(CityEntity entity, string batchNo, CityEntity cityResult, out DynamicParameters updateParameter)
        {
            string sql_cityChange = string.Empty;
            updateParameter = new DynamicParameters();

            sql_cityChange = SqlCommands.GetInsertSql("CityChange",
                new string[] { "BatchNo", "OldProvinceCode", "NewProvinceCode", "OldProvinceName", "NewProvinceName",
                    "OldCityCode", "NewCityCode", "OldCityName", "NewCityName", "OldCityLongitude", "NewCityLongitude", "OldCityLatitude", "NewCityLatitude",
                    "IsChange", "Type", "Content" });
            updateParameter.Add("IsChange", true, DbType.Boolean);
            updateParameter.Add("Content", "更新市信息 " + cityResult.CityCode + ":" + cityResult.CityName + " 为 " + entity.CityCode + ":" + entity.CityName, DbType.AnsiString);
            updateParameter.Add("?BatchNo", batchNo, DbType.AnsiString);
            updateParameter.Add("?OldProvinceCode", cityResult.ProvinceCode, DbType.AnsiString);
            updateParameter.Add("?NewProvinceCode", entity.ProvinceCode, DbType.AnsiString);
            updateParameter.Add("?OldProvinceName", cityResult.ProvinceName, DbType.AnsiString);
            updateParameter.Add("?NewProvinceName", entity.ProvinceName, DbType.AnsiString);
            updateParameter.Add("?OldCityCode", cityResult.CityCode, DbType.AnsiString);
            updateParameter.Add("?NewCityCode", entity.CityCode, DbType.AnsiString);
            updateParameter.Add("?OldCityName", cityResult.CityName, DbType.AnsiString);
            updateParameter.Add("?NewCityName", entity.CityName, DbType.AnsiString);
            updateParameter.Add("?OldCityLongitude", cityResult.Longitude, DbType.Double);
            updateParameter.Add("?NewCityLongitude", entity.Longitude, DbType.Double);
            updateParameter.Add("?OldCityLatitude", cityResult.Latitude, DbType.Double);
            updateParameter.Add("?NewCityLatitude", entity.Latitude, DbType.Double);
            updateParameter.Add("?Type", "City", DbType.AnsiString);
            return sql_cityChange;
        }

        public async Task<bool> UpdateIsEnabledAsync(string batchNo)
        {
            string insertSql = @"INSERT INTO CityChange(BatchNo,OldProvinceCode,OldProvinceName,OldProvinceLongitude,OldProvinceLatitude,
                                OldCityCode,OldCityName,OldCityLongitude,OldCityLatitude,IsDel,Type,Content) 
                                SELECT ?BatchNo,B.ProvinceCode,B.ProvinceName,B.Longitude,B.Latitude,A.CityCode,A.CityName,
                                A.Longitude,A.Latitude,?IsDel,?Type,?Content 
                                FROM City A LEFT JOIN Province B ON A.ProvinceCode = B.ProvinceCode WHERE A.IsEnabled = 0 ";
            DynamicParameters cityChangeParameters = new DynamicParameters();
            cityChangeParameters.Add("?BatchNo", batchNo, DbType.AnsiString);
            cityChangeParameters.Add("?IsDel", true, DbType.Boolean);
            cityChangeParameters.Add("?Type", "City", DbType.AnsiString);
            cityChangeParameters.Add("?Content", "写入未匹配上市信息", DbType.AnsiString);
            return await this.databaseContext.Master.ExecuteAsync(insertSql, cityChangeParameters, databaseContext.Transaction) != 0;
        }
    }
}
